package hero.mps.web.school_student.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import hero.mps.web.role_stu.entity.StuRole;
import hero.mps.web.role_stu.service.StuRoleService;
import hero.mps.web.school_student.entity.*;
import hero.mps.web.school_student.mapper.SchoolStudentMapper;
import hero.mps.web.school_student.service.SchoolStudentService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class SchoolStudentServiceImpl extends ServiceImpl<SchoolStudentMapper, SchoolStudent> implements SchoolStudentService {
    public final StuRoleService stuRoleService;

    @Override
    @Transactional
    public void saveStu(SchoolStudent schoolStudent) {
        int insert = this.baseMapper.insert(schoolStudent);
        if (insert > 0) {
            StuRole stuRole = new StuRole();
            stuRole.setRoleId(schoolStudent.getRoleId());
            stuRole.setStuId(schoolStudent.getStuId());
            stuRoleService.save(stuRole);
        }
    }

    @Override
    @Transactional
    public void updateStu(SchoolStudent schoolStudent) {
//        编辑学生
        int update = this.baseMapper.updateById(schoolStudent);
//        删除学生角色表格的角色，再重新插入
        if (update > 0) {
//            先删除
            QueryWrapper<StuRole> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(StuRole::getRoleId, schoolStudent.getStuId());
            stuRoleService.remove(queryWrapper);
//            在插入
            StuRole stuRole = new StuRole();
            stuRole.setRoleId(schoolStudent.getRoleId());
            stuRole.setStuId(schoolStudent.getStuId());
            stuRoleService.save(stuRole);
        }
    }

    @Override
    public SchoolStudent getStuById(Integer id) {
        return this.baseMapper.getStuById(id);
    }

    @Override
    public IPage<SchoolStudent> getList(StudentParam studentParam) {
        IPage<SchoolStudent> page = new Page(studentParam.getCurrentPage(), studentParam.getPageSize());
        return this.baseMapper.getList(page, studentParam);
    }

    @Override
    public IPage<MyCourseVO> getCourseList(CourseParamVO param) {
        IPage<MyCourseVO> page = new Page(param.getCurrentPage(), param.getPageSize());
        return this.baseMapper.getCourseList(page, param);
    }

    @Override
    @Transactional
    public void deleteStu(Integer stuId) {
        //        删除学生角色表
        QueryWrapper<StuRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(StuRole::getStuId, stuId);
        boolean delete = stuRoleService.remove(queryWrapper);
        if (delete) {
            //        删除学生
            this.baseMapper.deleteById(stuId);
        }

    }

    @Override
    public StuCount getStuCount() {
        List<StuCountVO> stuCountVO = this.baseMapper.getStuCount();
        StuCount  stuCount = new StuCount();
        List<Integer> count = new ArrayList<Integer>();
        List<String> names = new ArrayList<String>();
        for (int i = 0; i < stuCountVO.size(); i++) {
            count.add(stuCountVO.get(i).getStuCount());
            names.add(stuCountVO.get(i).getClassYear());
        }
        stuCount.setCount(count);
        stuCount.setNames(names);
        return stuCount;
    }

    @Override
    public List<HotMajorVO> getHotMajor() {
        return this.baseMapper.getHotMajor();
    }
}
